import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
# 第三方SMTP服务
mail_host = "smtp.qq.com"  # 设置服务器
# 自己的邮箱，通过QQ邮箱设置获取口令
mail_user = "2767268399@qq.com"  # 用户名
mail_pass = "codgkwwhhvghdcia"  # 口令

# 这里sender和mail_user要一致，腾讯可能不让匿名发送邮件
sender = '2767268399@qq.com'
receivers = ['2028695541@qq.com']  # 接受者的邮箱

# 创建一个带附件的实例
message = MIMEMultipart()
message['From'] = Header("神秘客", 'utf-8')
message['To'] = Header("2028695541@qq.com", 'utf-8')
subject = '收就完了'
message['Subject'] = Header(subject, 'utf-8')

# 邮件正文内容
message.attach(MIMEText('收就完了', 'plain', 'utf-8'))

#构造附件1，传送当前目录下的test.txt文件，可以看出是直接读取文本二进制流
att1 = MIMEText(open('Calc.html','rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="Calc.html"'
message.attach(att1)


try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_host, 25)  # 25为SMTP端口号
    smtpObj.login(mail_user, mail_pass)  # 会返回(状态码, "字符串解释")元组信息
    smtpObj.sendmail(sender, receivers, message.as_string())
    print(message)
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")
